package com.zbl.zblproject.pay.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zbl.zblproject.pay.entity.PayOrder;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/11/5
 */
@Repository
public interface PayOrderMapper extends BaseMapper<PayOrder> {

    /**
     * 查询对应订单的线上订单号
     * @param orderSn
     * @return
     */
    @Select("SELECT online_order_sn FROM zbl_pay_order WHERE online_order_sn = #{orderSn}")
    String selectOnlineOrderOfPayOrder(@Param("orderSn") String orderSn);

    @Select("SELECT id FROM zbl_pay_order WHERE id LIKE '${orderSn}%' ORDER BY id DESC LIMIT 1")
    Long selectFlowOrder(@Param("orderSn") String orderSn);


    @Select("SELECT * FROM zbl_pay_order WHERE TO_DAYS(create_time) = TO_DAYS(now()) AND status = 1 AND user_id = #{userId} order by create_time desc")
    List<PayOrder> getTodayOrderList(@Param("userId") int userId);

    List<PayOrder> getOrderListBySn(@Param("userId") int userId, @Param("orderSn") String orderSn);

    List<PayOrder> getOrderListBySns(@Param("userId") int userId, @Param("orderSn") String orderSn);

    @Select("SELECT * FROM zbl_pay_order WHERE  id = #{id}")
    PayOrder selectPayOrderById(@Param("id") long id);

    @Select("SELECT * FROM zbl_pay_order WHERE  id =#{id}")
    List<PayOrder> selectPayOrderByIdList(@Param("id") long id);

    @Select("SELECT COUNT(*) FROM zbl_pay_order WHERE status = 1 AND user_id = #{userId} AND  mode = #{mode} AND create_time >= #{start} AND create_time <= #{end}")
    Integer selectPayTimes(@Param("userId") Integer userId,
                           @Param("mode") Integer mode,
                           @Param("start") LocalDateTime start,
                           @Param("end") LocalDateTime end);

    @Select("SELECT IFNULL(SUM(official_receipts),0) FROM zbl_pay_order WHERE status = 1 AND user_id = #{userId} AND  mode = #{mode} AND create_time >= #{start} AND create_time <= #{end}")
    BigDecimal selectPayMoney(@Param("userId") Integer userId,
                              @Param("mode") Integer mode,
                              @Param("start") LocalDateTime start,
                              @Param("end") LocalDateTime end);

    List<PayOrder> selectOrderAll(@Param("userId") int userId,
                                           @Param("model") int model,
                                           @Param("start") LocalDateTime start,
                                           @Param("end") LocalDateTime end);

    List<PayOrder> selectTodayOrder(@Param("userId") int userId);
}
